#include<bits/stdc++.h>
using namespace std;
const int N = 3e5+10;
const int LMT = sqrt(3e5);
struct ACAM{
    int to[N][26];
    int fail[N];
    int cnt[N];
    int pcnt;
    int gen(){
        int now = ++pcnt;
        memset(to[now],0,sizeof(int)*26);
        fail[now] = 0;
        cnt[now] = 0;
        return now;
    }
    int rot;
    void init(){
        pcnt = 0;
        rot = gen();
    }
    void insert(string s,int val){
        int now = rot;
        for(char c : s){
            c -= 'a';
            if(!to[now][c]){
                to[now][c] = gen();
            }
            now = to[now][c];
        }
        cnt[now] += val;
    }
    void build(){
        fail[rot] = rot;
        queue<int> que;
        for(int i = 0;i < 26;i++){
            if(to[rot][i]){
                que.push(to[rot][i]);
                fail[to[rot][i]] = rot;
            }else{
                to[rot][i] = rot;
            }
        }
        while(que.size()){
            int now = que.front();
            que.pop();
            cnt[now] += cnt[fail[now]];
            for(int i = 0;i < 26;i++){
                if(to[now][i]){
                    que.push(to[now][i]);
                    fail[to[now][i]] = to[fail[now]][i];
                }else{
                    to[now][i] = to[fail[now]][i];
                }
            }
        }
    }
}acam;
int sp,lsp;
string S[N];
int Sval[N];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int m;
    cin>>m;
    int cs = 0;
    acam.init();
    acam.build();
    int chknum1 = 0,chknum2 = 0;
    for(int i = 1;i<=m;i++){
        int t;
        cin>>t;
        if(t <= 2){
            cin>>S[++sp];
            if(t == 1){
                Sval[sp] = 1;
            }else{
                Sval[sp] = -1;
            }
            cs += S[sp].size();
            if(cs > LMT){
                acam.init();
                for(int i = 1;i <= sp;i++){
                    acam.insert(S[i],Sval[i]);
                }   
                acam.build();
                cs = 0;
                lsp = sp;
                chknum1++;
            }
        }else{
            string in;
            cin>>in;
            int cur = acam.rot;
            int res = 0;
            for(int i = 0;i < in.size();i++){
                cur = acam.to[cur][in[i] - 'a'];
                res += acam.cnt[cur];
                for(int j = lsp + 1;j <= sp;j++){
                    if(S[j].size() <= i + 1){
                        bool f = 1;
                        for(int k = 0;k < S[j].size();k++){
                            if(S[j][k] != in[i - S[j].size() + 1 + k]){
                                f = 0;
                                chknum2++;
                                break;
                            }
                        }
                        if(f){
                            res += Sval[j];
                        }
                    }
                }
            }
            cout<<res<<endl;
        }
        if(i % 1000 == 0){
            cerr<<chknum1<<' '<<chknum2<<'\n';
        }
    }
}